import { useState, useEffect } from 'react'
import { checkIpWhitelist } from '@/services/ipWhitelist'

export function useIpWhitelist() {
  const [isAllowed, setIsAllowed] = useState<boolean | null>(null)
  const [isLoading, setIsLoading] = useState(true)

  useEffect(() => {
    const checkAccess = async () => {
      try {
        setIsLoading(true)
        const allowed = await checkIpWhitelist()
        setIsAllowed(allowed)
      } catch (error) {
        console.error('IP白名单检查失败:', error)
        setIsAllowed(false)
      } finally {
        setIsLoading(false)
      }
    }

    checkAccess()
  }, [])

  return { isAllowed, isLoading }
}
